package com.simblog.oaknt.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simblog.oaknt.mapper.BaseMapper;
import com.simblog.oaknt.service.IBaseService;

public abstract class BaseServiceImpl<T> implements IBaseService<T>{
	
	public abstract BaseMapper<T> getMapper();

	public int deleteByPrimaryKey(Long id) {
		return getMapper().deleteByPrimaryKey(id);
	}

	public int insert(T record) {
		return getMapper().insert(record);
	}

	public int insertSelective(T record) {
		return getMapper().insertSelective(record);
	}

	public List<T> queryList(Map<String, Object> params) {
		return getMapper().query(params);
	}

	public PageInfo<T> queryPage(Map<String, Object> params, PageInfo<T> pageInfo) {
		
		PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
		pageInfo = new PageInfo<T>(getMapper().query(params));
		return pageInfo;
	}

	public T selectByPrimaryKey(Long id) {
		return getMapper().selectByPrimaryKey(id);
	}

	public int updateByPrimaryKey(T record) {
		return getMapper().updateByPrimaryKey(record);
	}

	public int updateByPrimaryKeySelective(T record) {
		return getMapper().updateByPrimaryKeySelective(record);
	}

	public Long insertReId(T recode) {
		getMapper().insert(recode);
		
		Method method;
		try {
			method = recode.getClass().getMethod("getId", new Class[]{});
			Object object = method.invoke(recode);
			return (Long) object;
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
}
